<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: CustomerController.php 4929 2010-08-25 03:36:50Z jam-gate.vn $
 * @since		2.0.0
 */

class Customer_CustomerController extends Zend_Controller_Action 
{
	/* ========== Backend actions =========================================== */

	/**
	 * Add new customer
	 * 
	 * @return void
	 */
	public function addAction() 
	{	
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$customerDao = Tomato_Model_Dao_Factory::getInstance()->setModule('customer')->getCustomerDao();
		$customerDao->setDbConnection($conn);
		
		$nextId 	= 	$customerDao->getNextId();
		$this->view->assign("nextId",$nextId);		
		$customer = new stdClass();
		$customer->id		 		 = $nextId;
		$customer->shop_id 			 = Zend_Auth::getInstance()->getIdentity()->shop->id;
		$customer->name 			 = Tomato_Config::getConfig()->get("jam")->customer_name;
		$customer->tel				 = Tomato_Config::getConfig()->get("jam")->customer_tel;
		$customer->address			 = Tomato_Config::getConfig()->get("jam")->customer_address;
		$customer->birthday		     = "";
		$customer->sex				 = Tomato_Config::getConfig()->get("jam")->customer_sex;
		$customer->first_time 		 = "";
		$customer->last_time 		 = "";
		$customer->amount_time		 = 0;
		$customer->images		 	 = "";
		$customer->first_deposit	 = 0;
		$customer->remaining_money	 = 0;
		$customer->currency		 	 = Tomato_Config::getConfig()->get("jam")->customer_currency;
		$customer->employee_id 	 	 = Zend_Auth::getInstance()->getIdentity()->employee->id;
		$customer->note			 	 = Tomato_Config::getConfig()->get("jam")->customer_note;
		$customer->referrer_id 	 	 = null;
		$customer->email 	 		 = Zend_Auth::getInstance()->getIdentity()->employee->id;
		$customer->used_money	 	 = 0;
			
		$request  = $this->getRequest();
		
		$this->view->assign('lang', $request->getParam('lang'));
		
		if ($request->isPost()) {
			$user 	  = Zend_Auth::getInstance()->getIdentity();
			$customer->name 		= $request->getParam("name_vi_VN",$customer->name);
			$customer->tel		    = $request->getParam("tel",$customer->tel);
			$customer->address	    = $request->getParam("address",$customer->address);
			$customer->birthday	    = $request->getParam("birthday",$customer->birthday);
			$customer->sex		 	= $request->getParam("sex",$customer->sex);
			$customer->first_time	= $request->getParam("first_time",$customer->first_time);
			$customer->last_time    = $request->getParam("last_time",$customer->last_time);
			$customer->amount_time  = $request->getParam("amount_time",$customer->amount_time);
			$customer->images       = $request->getParam("images",$customer->images);
			$customer->first_deposit    = $request->getParam("first_deposit",$customer->first_deposit);
			$customer->remaining_money  = $request->getParam("remaining_money",$customer->remaining_money);
			$customer->currency 		= $request->getParam("currency",$customer->currency);
			$customer->employee_id 		= $request->getParam("employee_id",$customer->employee_id);
			$customer->note 			= $request->getParam("note",$customer->note);
			$customer->referrer_id 		= $request->getParam("referrer_id",$customer->referrer_id);
			$customer->email 			= $request->getParam("first_deposit",$customer->email);
			$customer->used_money		= $customer->first_deposit - $customer->remaining_money;
			
			$id = $customerDao->add($customer);
						
			$this->_helper->getHelper('FlashMessenger')
				->addMessage($this->view->translator('customer_add_success'));
			$this->_redirect($this->view->serverUrl() . $this->view->url(array("customer_id"=>$customer->id), 'customer_customer_edit'));
		}
		$this->view->assign("customer",$customer);		
	}
	
	/**
	 * Delete customer
	 * 
	 * @return void
	 */
	public function deleteAction() 
	{
		$this->_helper->getHelper('layout')->disableLayout();
		$this->_helper->getHelper('viewRenderer')->setNoRender();
		
		$response = 'RESULT_ERROR';
		$request  = $this->getRequest();
		if ($request->isPost()) {
			$id = $request->getPost('id');
			$conn = Tomato_Db_Connection::factory()->getMasterConnection();
			$customerDao = Tomato_Model_Dao_Factory::getInstance()->setModule('customer')->getCustomerDao();
			$customerDao->setDbConnection($conn);
			$customer = $customerDao->getById($id);
			
			if ($customer != null) {
				$customerDao->delete($customer);
				$response = 'RESULT_OK';
			}
		}
		$this->getResponse()->setBody($response);
	}
	
	/**
	 * Edit customer
	 * 
	 * @return void
	 */
	public function editAction() 
	{		  
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$customerDao = Tomato_Model_Dao_Factory::getInstance()->setModule('customer')->getCustomerDao();
		$customerDao->setDbConnection($conn);
		
		$request    = $this->getRequest();
		$customer_id = $request->getParam("customer_id",null);
		if($customer_id){
			$customer    		= $customerDao->getById($customer_id);
			$this->view->assign('lang', $request->getParam('lang'));			
			if ($request->isPost()) {
				$user 	  = Zend_Auth::getInstance()->getIdentity();
				$customer->name 		= $request->getParam("name_vi_VN",$customer->name);
				$customer->tel		    = $request->getParam("tel",$customer->tel);
				$customer->address	    = $request->getParam("address",$customer->address);
				$customer->birthday	    = $request->getParam("birthday",$customer->birthday);

				$customer->birthday     = Core_Services_Date::convert($customer->birthday);				
								 
				$customer->sex		 	= $request->getParam("sex",$customer->sex);
				
				$customer->first_time	= $request->getParam("first_time",$customer->first_time);
				$customer->first_time   = Core_Services_Date::convert($customer->first_time);
				
				$customer->last_time    = $request->getParam("last_time",$customer->last_time);
				$customer->last_time    = Core_Services_Date::convert($customer->last_time);				
				
				$customer->amount_time  = $request->getParam("amount_time",$customer->amount_time);
				$customer->images       = $request->getParam("images",$customer->images);
				$customer->first_deposit    = $request->getParam("first_deposit",$customer->first_deposit);
				$customer->remaining_money  = $request->getParam("remaining_money",$customer->remaining_money);
				$customer->currency 		= $request->getParam("currency",$customer->currency);
				$customer->employee_id 		= $request->getParam("employee_id",$customer->employee_id);
				$customer->note 			= $request->getParam("note",$customer->note);
				$customer->referrer_id 		= $request->getParam("referrer_id",$customer->referrer_id);
				$customer->first_deposit 	= $request->getParam("first_deposit",$customer->first_deposit);
				$customer->email 			= $request->getParam("email",$customer->email);
				$customer->used_money		= $customer->first_deposit - $customer->remaining_money;
													
				$id = $customerDao->update($customer);			
				$this->_helper->getHelper('FlashMessenger')
					->addMessage($this->view->translator('customer_edit_success'));
				$this->_redirect($this->view->serverUrl() . $this->view->url(array("customer_id"=>$customer->id), 'customer_customer_edit'));
			}
			$this->view->assign("customer",$customer);
		}	
	}
	
	/**
	 * List categories
	 * 
	 * @return void
	 */
	public function listAction() 
	{
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$customerDao = Tomato_Model_Dao_Factory::getInstance()->setModule('customer')->getCustomerDao();
		$customerDao->setDbConnection($conn);
		
		$lang = $this->getRequest()->getParam('lang', Tomato_Config::getConfig()->web->lang);
		
		$customerDao->setLang($lang);
        $request   = $this->getRequest();
		$pageIndex = $request->getParam('pageIndex', 1);
        $perPage   = Tomato_Config::getConfig()->manager->itemPerPage;;
        $offset    = ($pageIndex - 1) * $perPage;
        $exp = array(
        	   );
		$customers 	 = $customerDao->find($offset, $perPage, $exp);
		$this->view->assign('customers', $customers);
		
		$total       = $customerDao->count($exp);        
		$paginator = new Zend_Paginator(new Tomato_Utility_PaginatorAdapter($customers, $total));
        $paginator->setCurrentPageNumber($pageIndex);
        $paginator->setItemCountPerPage($perPage);
        
		$this->view->assign('paginator', $paginator);
        $this->view->assign('paginatorOptions', array(
            'path'        => $this->view->url(array(), 'customer_customer_list'),
            'itemLink' 	  => 'page-%d'                          
        ));
        $this->view->assign('lang',$lang);				
	} 		
	
	public function testAction(){
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$customerDao = Tomato_Model_Dao_Factory::getInstance()->setModule('customer')->getCustomerDao();
		$customerDao->setDbConnection($conn);
		
		$rows 	     = $customerDao->find(null,null,null);
		$this->view->assign("rows",$rows);
		$arrHeader    = array("id"=>"30%","name"=>"30%","address"=>"40%");
		$this->view->assign("arrHeader",$arrHeader);
		/*		
		$title 		  = "Lựa chọn khách hàng";
		$arrHeader    = array("id"=>"30%","name"=>"30%","address"=>"40%");
		$dialogWidth  = 600; // 600,auto
		$dialogheight = 300; // 300,auto
		$tableId	  = "ABC";
		$fnOpenDialog = "dialogSearch"; // tên hàm sẽ gọi để mở dialog		
		$fnTableActionRow   = "fnTableActionTestRow";
		$fnTableAction      = "fnTableActionTest";
		$typeTableAction = "checkbox";
		
		$this->view->assign("title",$title);					
		$this->view->assign("arrHeader",$arrHeader);
		$this->view->assign("rows",$rows);
		$this->view->assign("dialogWidth",$dialogWidth);
		$this->view->assign("dialogHeight",$dialogheight);
		$this->view->assign("fnOpenDialog",$fnOpenDialog);
		$this->view->assign("tableId",$tableId);
		$this->view->assign("typeTableAction",$typeTableAction); // checkbox, radio
		$this->view->assign("fnTableActionRow",$fnTableActionRow);
		$this->view->assign("fnTableAction",$fnTableAction);
		*/
	}
}	